package com.zhuangqi.algorithm.base;

import java.util.Arrays;

import static com.zhuangqi.algorithm.template.BaseTemplate.exch;
import static com.zhuangqi.algorithm.template.BaseTemplate.less;

/**
 * 插入排序
 *
 * 5 3 7 2 9 1
 * 3 5 7 2 9 1
 * 3 5 2 7 9 1
 * 3 2 5 7 9 1
 * 2 3 5 7 9 1
 */
public class Insertion {

    public static void sort(Comparable[] a) {
        for (int i = 1; i < a.length; i++) {
            for (int j=i; j>0 && less(a[j],a[j-1]); j--) {
                exch(a,j,j-1);
            }
        }
    }

    public static void main(String[] args) {
        Integer[] a = {4,6,7,8,1,5,10,2,8,3};
        sort(a);
        System.out.println(Arrays.toString(a));
    }
}
